var ns;
app = new Vue({
    el: '#app',
    data: {
        show_detail: true,
        OwnerName: '',
        show_history: false,
        show_detail_haslist: true,
        show_history_haslist: true,
        EndTime: '',
        is_active1: '',
        is_active2: '',
        money_pay_num: 0,
        pay_type: 0,
        idlist: '',
        is_QrCode: false,
        is_mask: false,
        QrCodeUrl: '',
        is_under_pay: true,
        TradeNo: 0,
        pay_succeed: false,
        pay_Interval: null,
        is_wx: false,
        is_zfb: false,
        FullName:'',
        content:'',
    },
    methods: {
        get_data: function() {
          //获取付费账单
          var that = this;
          ns.post({
              action: 'getqrpayroomfeelist',
              idlist: that.idlist,
          }, function(succeed, data, err) {
              if (succeed) {
                  that.content = data.content;
              } else {
                  api.toast({
                      msg: err,
                      duration: 2000,
                      location: 'bottom'
                  });
              }
          });
        },
        do_generate: function() {
            var that = this;
            if (that.money_pay_num == 0) {
                api.toast({
                    msg: '金额为0不能支付',
                    duration: 2000,
                    location: 'bottom'
                });
                return
            }
            if (that.pay_type == 0) {
                api.toast({
                    msg: '请选择支付方式',
                    duration: 2000,
                    location: 'bottom'
                });
            } else if (that.pay_type == 1) {
                ns.post({
                    action: 'alipayorderbyqrcodeready',
                    idlist: that.idlist,
                }, function(succeed, data, err) {
                    if (succeed) {
                        that.is_QrCode = true;
                        that.QrCodeUrl = data.QrCodePath;
                        that.TradeNo = data.TradeNo;
                        that.is_under_pay = false;
                        that.is_zfb = true;
                        that.is_wx = false;
                        that.confirm_pay();
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
            } else if (that.pay_type == 2) {
                ns.post({
                    action: 'wxpayorderbyqrcodeready',
                    idlist: that.idlist,
                }, function(succeed, data, err) {
                    if (succeed) {
                        that.is_QrCode = true;
                        that.QrCodeUrl = data.QrCodePath;
                        that.is_under_pay = false;
                        that.TradeNo = data.TradeNo;
                        that.is_wx = true;
                        that.is_zfb = false;
                        //that.is_mask = true;
                        that.confirm_pay();

                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
            } else if (that.pay_type == 3) {
                api.toast({
                    msg: '请使用现金付款',
                    duration: 2000,
                    location: 'bottom'
                });
            }
        },
        confirm_pay: function() {
            var that = this;
            ns.post({
                action: 'checkqrcodepaystatus',
                TradeNo: that.TradeNo,
            }, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '付款成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.pay_succeed = true;
                    that.is_QrCode = false;
                    api.sendEvent({
                        name: 'updatewuyejf',
                        extra: {
                            key1: 'value1',
                        }
                    });
                } else {
                    that.pay_Interval = setTimeout(function() {
                        that.confirm_pay();
                    }, 2000);
                }
            });
        },
        cancel_pay: function() {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确认取消付款？',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    that.is_QrCode = false;
                    that.is_under_pay = true;
                    api.toast({
                        msg: '已经取消本次付款',
                        duration: 2000,
                        location: 'bottom'
                    });
                    if (that.pay_Interval != null) {
                        clearTimeout(that.pay_Interval);
                    }
                }
            });
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.money_pay_num = api.pageParam.id || 0;
    app.idlist = api.pageParam.idlist || '';
    app.FullName = api.pageParam.name || '';
    app.get_data();
};
